﻿using Niusys.Entities;
using Odn.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Odn.Entities;
using NHibernate;
using NHibernate.Linq;

namespace Niusys.Repostories
{
    public interface IAuthenticationRepository
    {
        void AddUserDevice(UserDevice existsDevice);
        UserDevice GetUserDevice(string sessionKey);
        UserDevice GetUserDevice(Guid userId, int deviceType);
        void UpdateUserDevice(UserDevice userDevice);
    }

    public class AuthenticationRepository : BaseRepository, IAuthenticationRepository
    {
        public void AddUserDevice(UserDevice existsDevice)
        {
            Session.Persist(existsDevice);
            Session.Flush();
        }

        public UserDevice GetUserDevice(string sessionKey)
        {
            return Session.Query<UserDevice>().SingleOrDefault(x => x.SessionKey == sessionKey);
        }

        public UserDevice GetUserDevice(Guid userId, int deviceType)
        {
            return Session.Query<UserDevice>().SingleOrDefault(x => x.UserId == userId && x.DeviceType == deviceType);
        }

        public void UpdateUserDevice(UserDevice userDevice)
        {
            Session.Update(userDevice);
            Session.Flush();
        }
    }
}
